package com.scau.XFourPOS.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.scau.XFourPOS.dao.BaseDao;
import com.scau.XFourPOS.utils.PageUtils;

/**
 * 类名称：BaseDaoImpl<T> 类描述：dao泛型类，实现了泛型接口，所有的dao类都继承这个类，实现基本的CRUD操作和简单的分页操作.
 * 创建人：庄祥鹏
 * */
public class BaseDaoImpl<T> implements BaseDao<T> {

	private Class<T> entityClass;
	private String className;
	@Resource
	private SessionFactory sessionFactory;

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@SuppressWarnings("unchecked")
	// 就是要获取E的真实数据类型
	public BaseDaoImpl() {
		this.entityClass = (Class<T>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
		className = entityClass.getSimpleName();
	}

	/**
	 * 保存或修改.
	 * 
	 * @param entity
	 */
	public void saveEntity(T entity) {
		getSession().save(entity);
	}

	public void updateEntity(T entity) {
		getSession().update(entity);
	}

	/**
	 * 删除对象.
	 * 
	 * @param entity
	 */
	public void deleteEntity(T entity) {
		getSession().delete(entity);
	}

	/**
	 * 按id删除对象.
	 * 
	 * @param id
	 */
	public void deleteEntityById(int id) {
		deleteEntity(loadEntityById(id));
	}

	/**
	 * 按id获取特定类的对象.
	 * 
	 * @param id
	 * @param entityClass
	 * @return 对象
	 */
	@SuppressWarnings("unchecked")
	public T loadEntity(final Class<T> entityClass, final int id) {
		return (T) getSession().load(entityClass, id);
	}

	/**
	 * 按id获取对象.
	 * 
	 * @param id
	 * @return 对象
	 */
	public T loadEntityById(int id) {
		return (T) loadEntity(entityClass, id);
	}

	/**
	 * 获取全部对象
	 * 
	 * @return list
	 */
	public List<T> loadAll() {
		Session session = getSession();
		Query query = session.createQuery("from " + className + "");
		return query.list();
	}
	
	/**
	 * 分页功能
	 * 
	 * @param pageUtils
	 * @return pageutils
	 */
	public PageUtils<T> loadAllByPage(PageUtils<T> pageUtils) {
		Session session = getSession();
		Query query = session.createQuery("from " + className + "");
		query.setFirstResult(pageUtils.getFirst());
		query.setMaxResults(pageUtils.getPageSize());
		pageUtils.setResult(query.list());
		pageUtils.setTotalCount(getTotalCount());
		return pageUtils;
	}
	/**
	 * 分页查询功能
	 * 
	 * @param pageUtils
	 * @param key
	 * @param value
	 * @return pageutils
	 */
	public PageUtils<T> loadByPageAndKey(PageUtils<T> pageUtils,String key,int value) {
		Session session = getSession();
		Query query = session.createQuery("from " + className +" u where u."+key+".id='"+value+"'");
		query.setFirstResult(pageUtils.getFirst());
		query.setMaxResults(pageUtils.getPageSize());
		pageUtils.setResult(query.list());
		pageUtils.setTotalCount(getTotalCountByKey(key,value));
		return pageUtils;
	}
	/**
	 * 获得总页数
	 * @return count
	 */
	public long getTotalCount(){
		Session session=getSession();
		Query query = session.createQuery("select count(*) from " + className +"");
		long count=(long) query.uniqueResult();
		return count;
	}
	/**
	 * 获得分页查询总页数
	 * @return count
	 */
	public long getTotalCountByKey(String key,int value){
		Session session=getSession();
		Query query = session.createQuery("select count(*) from " + className +" u where u."+key+".id='"+value+"'");
		long count=(long) query.uniqueResult();
		return count;
	}
	/**
	 * Flush当前Session.
	 */
	public void flush() {
		getSession().flush();
	}

	@Override
	public boolean isExitEntity(String key,String value) {
		Session session = getSession();
		Query query = session.createQuery("from " + className +" u where u."+key+"='"+value+"'");
		List productList=query.list();
		if(productList.size()==0)
			return false;
		return true;
	}

	@Override
	public T loadEntityByKey(String key, String value) {
		Session session = getSession();
		Query query = session.createQuery("from " + className +" u where u."+key+"='"+value+"'");
		List list=query.list();
		return (T) list.get(0);
	}

}